package com.atguigu.ocp;

/**
 * 没有按照开闭原则设计的代码
 */
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}

/**
 * 这是一个用于绘图的类 [使用方]
 */
class GraphicEditor {
    /**
     * 接收一个Shape对象,根据Shape的m_type属性来绘制不同的图形
     *
     * @param shape {@link Shape}
     */
    public void drawShape(Shape shape) {
        if (shape.m_type == 1) {
            drawRectangle(shape);
        } else if (shape.m_type == 2) {
            drawCircle(shape);
        } else if (shape.m_type == 3) { //这里就是使用方修改了代码
            drawTriangle(shape);
        }
    }

    private void drawRectangle(Shape shape) {
        System.out.println("绘制矩形");
    }

    private void drawCircle(Shape shape) {
        System.out.println("绘制圆形");
    }

    /**
     * 这里就是使用方修改了代码
     *
     * @param shape {@link Triangle}
     */
    private void drawTriangle(Shape shape) {
        System.out.println("绘制三角形");
    }
}

/**
 * Shape类 基类
 */
class Shape {
    int m_type;
}

/**
 * 矩形类
 */
class Rectangle extends Shape {
    public Rectangle() {
        super.m_type = 1;
    }
}

/**
 * 圆形类
 */
class Circle extends Shape {
    public Circle() {
        super.m_type = 2;
    }
}

/**
 * 三角形类
 */
class Triangle extends Shape {
    public Triangle() {
        super.m_type = 3;
    }
}
